/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.bl.LibroBlLocal;
import mx.its.dsw2.biblioteca.modelo.Autor;
import mx.its.dsw2.biblioteca.modelo.Categoria;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author Juan Carlos
 */
@ManagedBean
@SessionScoped
public class BookBean {

    //-------------------------EJB's--------------------------------------------
    @EJB
    private LibroBlLocal libroBl;
    @EJB
    private AutorBlLocal autorBl;
    @EJB
    private CategoriaBlLocal categoriaBl;
    //--------------------------------------------------------------------------
    //----------------------------VARIABLES-------------------------------------
    private List<Libro> lista;
    private List<Categoria> caterogyList;
    private List<Autor> authorList;
    private Libro book;
    private Categoria category;
    private String action;
    private String link;
    private int authorSelected;
    private int categorySelected;

    //--------------------------------------------------------------------------
    //---------------------Constructores----------------------------------------
    public BookBean() {
        book = new Libro();
    }
    //--------------------------------------------------------------------------

    //----------------------GETTER and SETTER-----------------------------------
    public List<Libro> getLista() {
        lista = libroBl.listAll();
        return lista;
    }

    public Libro getBook() {
        return book;
    }

    public void setBook(Libro book) {
        this.book = book;
    }

    public List<Categoria> getCaterogyList() {
        
        return caterogyList;
    }

    public List<Autor> getAuthorList() {
        return authorList;
    }
    
    

    //--------------------------------------------------------------------------
    //----------------Acciones--------------------------------------------------
    public String procesarSeleccion() {
        book.setIdAutor(autorBl.findById(authorSelected));
        return "BookConfirm";
    }

    public String procesarAcion() {
        if (action.equalsIgnoreCase("new")) {
            libroBl.register(book, caterogyList);
        } else if (action.equalsIgnoreCase("edit")) {
            libroBl.modify(book,caterogyList);
        } else if (action.equalsIgnoreCase("delete")) {
            libroBl.remove(book);
        }
        action = "";
        return "BookList";
    }

    public void prepareNew(ActionEvent evt) {
        book = new Libro();
        action = "New";
        caterogyList = new ArrayList<Categoria>();
    }

    public void prepararNuevo(ActionEvent evt) {
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        book = lista.get(index);
        action = "New";
    }
    
    public void eliminarCategoriaLista(ActionEvent evt){
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        caterogyList.remove(index);
    }

    public void prepararEliminar(ActionEvent evt){
       action="Eliminar";
       int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       book=lista.get(index);     
    }
    
    public void preparaEditar(ActionEvent evt){
        int indice = Integer.parseInt(evt.getComponent().getClientId());
        book = lista.get(indice);
        caterogyList = categoriaBl.getListByBook(book);
        action = "Edit";
    }
    
    public String llamadaCancelarEditar(){     
       if(action.equalsIgnoreCase("Editar")||action.equalsIgnoreCase("Nuevo")
               ||action.equalsIgnoreCase("Eliminar2")){
           link="BookCreateEdit";
           
       }else{
           if(action.equalsIgnoreCase("Eliminar")){
               link="BookList";
           }
       }
      return link;    
    }
    
    public void addCategory(ActionEvent evt) {
        caterogyList.add(categoriaBl.findById(categorySelected));
    }
    //--------------------------------------------------------------------------
}
